#需求：爬取58二手房中的房源信息
import requests
from lxml import etree
if __name__ == "__main__":
    # UA伪装：将对应的User-Agent封装到一个字典
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
    }
    url = 'https://bj.58.com/ershoufang/'
    page_text = requests.get(url=url,headers=headers).text
    #数据解析
    tree = etree.HTML(page_text)
    #存储的事select标签对象
    li_list = tree.xpath('//h3[@class="property-content-title-name"]/text()')
    fp = open('58.txt','w',encoding='utf-8')
    for i in li_list:
        title = i.xpath('./h3/text()')[0]
        print(title)
        fp.write(i+'\n')
